package fti.uksw.ruang.dialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import fti.uksw.ruang.R;
import fti.uksw.ruang.model.RecurringDetail;
//import android.app.DialogFragment;
//import android.content.ClipData.Item;

public class MenuRepeatEventDialog extends DialogFragment {

	private static final String TAG = MenuRepeatEventDialog.class.getName();

	static Context mContext;
	static MenuRepeatEventDialogListener mListener;

	private ListView lv;
	private Button btnSet;
	private Button btnCancel;
	private List<String> list;
	private AdapterMenuRepeatEventDialog adapter;

	static Calendar calWaktuBerhenti = Calendar.getInstance();

	public static MenuRepeatEventDialog newInstance(Context context,
			long mWaktuBerhenti) {
		MenuRepeatEventDialog menuRepeatDialog = new MenuRepeatEventDialog();
		mContext = context;
		calWaktuBerhenti.setTimeInMillis(mWaktuBerhenti);
		return menuRepeatDialog;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		list = new ArrayList<String>();
		list.add(RecurringDetail.MODE_DAILY);
		list.add(RecurringDetail.MODE_WEEKLY);
		list.add(RecurringDetail.MODE_MONTHLY);

		adapter = new AdapterMenuRepeatEventDialog(list, getActivity());
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.menurepeat, viewGroup, false);
		getDialog().setTitle("Repeat");

		lv = (ListView) v.findViewById(R.id.lv_menurepeat_menu);

		lv.setAdapter(adapter);
		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lv.setOnItemClickListener(lvItemClickListener);

		btnSet = (Button) v.findViewById(R.id.btn_menurepeat_set);
		btnSet.setOnClickListener(btnSetClickListener);

		btnCancel = (Button) v.findViewById(R.id.btn_menurepeat_cancel);
		btnCancel.setOnClickListener(btnCancelClickListener);
		return v;
	}

	private String itemSelected;

	private final OnItemClickListener lvItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapter, View view,
				int position, long id) {
			itemSelected = adapter.getItemAtPosition(position).toString();
			mListener.MenuItemSelected(itemSelected);

			DetailMenuRepeatEventDialog dmred = DetailMenuRepeatEventDialog
					.newInstance(itemSelected, calWaktuBerhenti);
			dmred.setDetailMenuRepeatEventDialogListener(new DetailMenuRepeatEventDialog.DetailMenuRepeatEventDialogListener() {

				@Override
				public void detailItemSelected(String item) {
					setItemSelected(item);
					mListener.ItemSelected(item);
				}
			});
			dmred.show(getFragmentManager(), "Detail menu repeat");
			dismiss();
		}
	};

	public void setItemSelected(String string) {
		itemSelected = string;
	}

	private final OnClickListener btnSetClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (itemSelected != null) {
				mListener.ItemSelected(itemSelected);
			}
//			dismiss();
		}
	};

	private final OnClickListener btnCancelClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			dismiss();
		}
	};

	public void setMenuRepeatEventDialogListener(
			MenuRepeatEventDialogListener listener) {
		mListener = listener;
	}

	public interface MenuRepeatEventDialogListener {
		public void MenuItemSelected(String item);

		public void ItemSelected(String item);
	}

	public class AdapterMenuRepeatEventDialog extends BaseAdapter {

		List<String> list;
		Activity a;
		LayoutInflater inflater;

		public AdapterMenuRepeatEventDialog(List<String> mList, Activity mA) {
			list = mList;
			a = mA;
			inflater = (LayoutInflater) a
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.room_listview, null);
			}
			String item = list.get(position);

			TextView tvRoomName = (TextView) convertView
					.findViewById(R.id.tv_newroomlv_roomname);
			tvRoomName.setText(item);
			return convertView;
		}

	}

	public static class DetailMenuRepeatEventDialog extends DialogFragment {

		public int repeatTime = 10;
		private DetailMenuRepeatEventDialogListener mListener;

		private DetailMenuRepeatEventAdapter adapterDetail;

		static Calendar waktuBerhenti = Calendar.getInstance();

		public static DetailMenuRepeatEventDialog newInstance(String title,
				Calendar mWaktuBerhenti) {
			DetailMenuRepeatEventDialog detailDialog = new DetailMenuRepeatEventDialog();
			Bundle args = new Bundle();
			args.putString("title", title);
			detailDialog.setArguments(args);
			waktuBerhenti = mWaktuBerhenti;
			return detailDialog;
		}

		public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup,
				Bundle savedInstanceState) {

			LinearLayout ll = new LinearLayout(getActivity());
			ll.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT));
			ll.setOrientation(LinearLayout.VERTICAL);

			ListView lv = new ListView(getActivity());

			LinearLayout llBottom = new LinearLayout(getActivity());
			llBottom.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT, 1));
			llBottom.setOrientation(LinearLayout.HORIZONTAL);

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT, 1);
			params.weight = 1;

			Button btnSet = new Button(getActivity());
			btnSet.setWidth(0);
			btnSet.setLayoutParams(params);
			btnSet.setText("Set");
			btnSet.setOnClickListener(btnSetClickListener);
			llBottom.addView(btnSet);

			Button btnCancel = new Button(getActivity());
			btnCancel.setWidth(0);
			btnCancel.setLayoutParams(params);
			btnCancel.setText("Cancel");
			btnCancel.setOnClickListener(btnCancelClickListener);
			llBottom.addView(btnCancel);

			List<String> listDetailMenu = new ArrayList<String>();
			listDetailMenu.add(repeatTime + " times");

			SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
			// Calendar c = Calendar.getInstance();
			listDetailMenu.add("until " + sdf.format(waktuBerhenti.getTime()));

			listDetailMenu.add("for " + repeatTime + " years");

			adapterDetail = new DetailMenuRepeatEventAdapter(getActivity(),
					listDetailMenu);

			lv.setAdapter(adapterDetail);
			lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			lv.setOnItemClickListener(itemClickListener);

			getDialog().setTitle(getArguments().getString("title"));
			ll.addView(lv);
			ll.addView(llBottom);
			return ll;
		}

		String detailItemSelected;

		private final OnClickListener btnSetClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (detailItemSelected != null) {
					mListener.detailItemSelected(detailItemSelected);
				}
				dismiss();
			}
		};

		private final OnClickListener btnCancelClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		};

		private Calendar setCalendarBaseOnModeRepeat(String mMode,
				Calendar mCalendar) {
			Calendar c = mCalendar;

			if (mMode == RecurringDetail.MODE_DAILY) {
				c.add(Calendar.DAY_OF_MONTH, 10);
			} else if (mMode == RecurringDetail.MODE_WEEKLY) {
				c.add(Calendar.WEEK_OF_MONTH, 10);
			} else if (mMode == RecurringDetail.MODE_MONTHLY) {
				c.add(Calendar.MONTH, 10);
			}
			return c;
		}

		private final OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> aa, View view, int pos,
					long id) {

				final SimpleDateFormat sdf = new SimpleDateFormat(
						"MMM dd, yyyy");

				String mode = getArguments().getString("title");

				// 0 = 10 time
				// 1 = date
				// 2 = 10 years
//				Calendar c = Calendar.getInstance();
				if (pos == 0) {
					waktuBerhenti = setCalendarBaseOnModeRepeat(mode, waktuBerhenti);
					setDetailItemSelected(aa.getItemAtPosition(pos).toString(), waktuBerhenti);
					mListener.detailItemSelected(detailItemSelected);
//					setDetailItemSelected(aa.getItemAtPosition(pos).toString(), c);
				}

				else if (pos == 1) {
					DateTimeDialog dtd = DateTimeDialog.newInstance(mContext,
							DateTimeDialog.DATEDIALOG, waktuBerhenti);
//					Log.d(TAG, sdf.format(waktuBerhenti.getTime()));
					dtd.setDateTimeDialogFragmentListener(new DateTimeDialog.DateTimeDialogFragmentListener() {

						@Override
						public void dateDialogFragmentDateTimeSet(
								Calendar calendar) {

							adapterDetail.updateNewDate(sdf.format(calendar.getTime()));
							adapterDetail.notifyDataSetChanged();
//							Log.d(TAG, sdf.format(calendar.getTime() + "-"+adapter.getItem(1)));
//							setDetailItemSelected(adapter.getItem(1), calendar);
//							mListener.detailItemSelected(detailItemSelected);
							waktuBerhenti = calendar;
							setDetailItemSelected(adapterDetail.getItem(1), waktuBerhenti);
							mListener.detailItemSelected(detailItemSelected);
						}
					});
					dtd.show(getFragmentManager(), "Date dialog");
				} else if (pos == 2) {
					waktuBerhenti = setCalendarBaseOnModeRepeat(mode, waktuBerhenti);
					setDetailItemSelected(aa.getItemAtPosition(pos).toString(), waktuBerhenti);
					mListener.detailItemSelected(detailItemSelected);
//					setDetailItemSelected(aa.getItemAtPosition(pos).toString(), c);
				}

				dismiss();
			}
		};

		public void setDetailItemSelected(String string, Calendar c) {
			detailItemSelected = getArguments().getString("title") + "-"
					+ string + "-" + c.getTimeInMillis();
		}

		public interface DetailMenuRepeatEventDialogListener {
			public void detailItemSelected(String item);
		}

		public void setDetailMenuRepeatEventDialogListener(
				DetailMenuRepeatEventDialogListener listener) {
			mListener = listener;
		}

		public class DetailMenuRepeatEventAdapter extends BaseAdapter {

			private List<String> mList;
			LayoutInflater inflater;

			public DetailMenuRepeatEventAdapter(Activity a , List<String> list) {
//				list = mList;
//				a = mA;
				inflater = (LayoutInflater) a
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//				super(mContext,	list);
				mList = list;
			}

			public void updateNewDate(String newItem) {
				mList.remove(1);
				mList.add(1, newItem);
			}

			@Override
			public int getCount() {
				return mList.size();
			}

			@Override
			public String getItem(int position) {
				return mList.get(position);
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				if (convertView == null) {
					convertView = inflater.inflate(R.layout.room_listview, null);
				}
				
				TextView tv = (TextView) convertView.findViewById(R.id.tv_newroomlv_roomname);
				tv.setText(mList.get(position));
				return convertView;
			}
		}
	}
}
